<?php
namespace Home\Controller;

class PayController extends HomeController{
    /**
     * 支付结果
     */
    public function pay(){
        if(IS_POST){
            !is_login() && $this->error('先登录',U('User/login'));
    
            $M_order = M('order');
            $M_payment_log = M('payment_log');
            $M_checkup = M('checkup');
    
            $paycode = I('post.paycode');
            if($paycode == 'balancePay'){
                $this->balancePay();
                exit;
            }
            $price = I('post.price');
            $order_no = I('post.order_no');
            $checkup_no = I('post.checkup_no');
            $order_id = I('post.order_id');
            $checkup_id = I('post.checkup_id');            
           
            $price = I('post.price');
            ! is_numeric($price) && $this->error('金额错误');
            (int) $price < 0 && $this->error('金额错误');
    
            if (empty($checkup_no) && empty($order_no)) {
                $this->error('订单号或鉴定单号不存在');
            }
    
            $payment_log_data = array();
    
            $payment_log_data['uid'] = UID;
            $payment_log_data['create_time'] = NOW_TIME;
            $payment_log_data['method'] = '+';
            $payment_log_data['payment_code'] = $paycode;
            $payment_log_data['status'] = 1;
            $payment_log_data['price'] = $price;
            $payment_log_data['transaction_no'] = rand(10000000, 99999999);
    
            $bill_data = array();
            $bill_data['uid'] = UID;
            $bill_data['create_time'] = NOW_TIME;
            $bill_data['money'] = $price;
            $bill_data['sign'] = '-';
            $bill_data['type'] = 2;
            $bill_data['status'] = 1;
    
            // 订单支付
            if ($order_id) {
                /* 检查订单状态 */
                is_array($order_id) && $order_id = implode(',', $order_id);
                is_array($order_no) && $order_no = implode(',', $order_no);
    
                $val_map = array();
                $val_map['id'] = array(
                        'in',
                        $order_id
                );
                $val_map['status'] = array(
                        'EGT',
                        1
                );
                $M_order->where($val_map)->find() && $this->error('订单中出现重复支付,请重试');
    
                $payment_log_data['order_id'] = $order_id;
                $payment_log_data['order_no'] = $order_no;
                $payment_log_data['log'] = '订单：' . $order_no . ' 支付'. ' 支付方式:'.$paycode;
    
                $bill_data['title'] = '订单支付' . $order_no;
                $bill_data['desc'] = '订单号：' . $order_no . ' 金额:' . $price . ' 支付方式:'.$paycode ;
    
                $map = array();
                $map['id'] = array(
                        'in',
                        $order_id
                );
    
                if (is_array($order_id) || is_array($order_no)) {
                    // count($order_id) != count($order_no) &&
                    // response(-1,'','订单与价格匹配错误');
                }
    
                // $sum=$M_order->where($map)->Sum('price');
    
                // (double)$sum !=(double)$price && response(-1,'','订单价格不一致');
                $payment_log_id = $M_payment_log->add($payment_log_data);
                if ($payment_log_id) {
                    M('bill')->add($bill_data);
                    $result = $M_order->where($map)->save(array(
                            'pay_time'=>NOW_TIME,
                            'status' => 1,
                            'payment_log_id' => $payment_log_id
                    ));
                    addMessage(2,'付款通知','您的订单'.$order_no.'支付成功',UID);
                    $this->success('支付成功',U('order/payresult'));
                } else {
                    $this->error('支付遇到问题');
                }
            }
            /* 我的鉴定支付 */
            else {
                // 检查鉴定状态
                is_array($checkup_id) && $checkup_id = implode(',', $checkup_id);
                is_array($checkup_no) && $checkup_no = implode(',', $checkup_no);
    
                $val_map = array();
                $val_map['id'] = array(
                        'in',
                        $checkup_id
                );
                $val_map['status'] = array(
                        'EGT',
                        1
                );
                $M_checkup->where($val_map)->find() && $this->error('鉴定单中出现重复支付,请重试');
    
                $payment_log_data['checkup_id'] = $checkup_id;
                $payment_log_data['checkup_no'] = $checkup_no;
                $payment_log_data['log'] = '鉴定单号：' . $checkup_no . '支付' . ' 支付方式:'.$paycode;
    
                $bill_data['title'] = '鉴定支付' . $checkup_no;
                $bill_data['desc'] = '鉴定单号：' . $checkup_no . ' 金额:' . $price . ' 支付方式:'.$paycode; 
    
                $map = array();
                $map['id'] = array(
                        'in',
                        $checkup_id
                );
    
                if (is_array($checkup_id) || is_array($checkup_no)) {
                    // count($checkup_id) != count($checkup_no) &&
                    // response(-1,'','鉴定单与价格匹配错误');
                }
    
                // $sum=$M_checkup->where($map)->Sum('price');
    
                // (double)$sum !=(double)$price && response(-1,'','鉴定单价格不一致');
                $payment_log_id = $M_payment_log->add($payment_log_data);
                if ($payment_log_id) {
                    M('bill')->add($bill_data);
                    $map = array();
                    $map['id'] = array(
                            'in',
                            $checkup_id
                    );
    
                    $M_checkup->where($map)->save(array(
                            'pay_time'=>NOW_TIME,
                            'status' => 1
                    ));
                    addMessage(2,'付款通知','您的鉴定单'.$checkup_no.'支付成功',UID);
                    $this->success('支付成功',U('order/payresult'));
                } else {
                    $this->error('支付遇到问题');
                }
            }
        }
    }
    

    /**
     * 余额支付
     *
     * @param
     *            int order_id
     * @param
     *            string order_no
     * @param
     *            double price
     */
    public function balancePay()
    {
        ! is_login() && $this->error('未登录');
    
        $order_id = I('post.order_id');
        $order_no = I('post.order_no');
        $money = I('post.price');
        $checkup_no = I('post.checkup_no');
        $checkup_id = I('post.checkup_id');
    
        $price = I('post.price');
        ! is_numeric($price) && $this->error('金额错误');
        (int) $price < 0 && $this->error('金额错误');
    
        if (empty($checkup_no) && empty($order_no)) {
           $this->error('订单号或鉴定单号不存在');
        }
    
        $M_order = M('order');
        $M_payment_log = M('payment_log');
        $M_member = M('member');
        $M_checkup = M('checkup');
    
        $member_info = $M_member->where(array(
                'uid' => UID
        ))->find();
    
        $payment_log_data = array();
        $paycode='balancepay';
        $payment_log_data['uid'] = UID;
        $payment_log_data['create_time'] = NOW_TIME;
        $payment_log_data['method'] = '+';
        $payment_log_data['payment_code'] = $paycode;
        $payment_log_data['status'] = 1;
        $payment_log_data['price'] = $price;
        $payment_log_data['transaction_no'] = rand(10000000, 99999999);
        
        $bill_data = array();
        $bill_data['uid'] = UID;
        $bill_data['create_time'] = NOW_TIME;
        $bill_data['sign'] = '-';
        $bill_data['type'] = 2;
        $bill_data['money'] = $money;
        $bill_data['status'] = 1;
    
        /* 订单支付 */
        if ($order_id) {
            // 检查订单状态
            is_array($order_id) && $order_id = implode(',', $order_id);
            is_array($order_no) && $order_no = implode(',', $order_no);
    
            $val_map = array();
            $val_map['id'] = array(
                    'in',
                    $order_id
            );
            $val_map['status'] = array(
                    'EGT',
                    1
            );
            $M_order->where($val_map)->find() && $this->error('订单中出现重复支付,请重试');
    
            (double) $money > (double) $member_info['amount'] && $this->error( '余额不足');
            $result = $M_member->where(array(
                    'uid' => UID
            ))->setDec('amount', $money);
    
            if ($result) {
    
                $payment_log_data['order_id'] = $order_id;
                $payment_log_data['order_no'] = $order_no;
                $payment_log_data['log'] = '订单：' . $order_no . ' 支付'. ' 支付方式:'.$paycode;
    
                $bill_data['title'] = '订单支付' . $order_no;
                $bill_data['desc'] = '订单号：' . $order_no . ' 金额:' . $price . ' 支付方式:'.$paycode ;
                
                $payment_log_id = $M_payment_log->add($payment_log_data);
                M('bill')->add($bill_data);
               
                $map = array();
                $map['id'] = array(
                        'in',
                        $order_id
                );
    
                $M_order->where($map)->save(array(
                        'pay_time'=>NOW_TIME,
                        'status' => 1,
                        'payment_log_id' => $payment_log_id
                ));
                addMessage(2,'付款通知','您的订单'.$checkup_no.'支付成功',UID);
                $this->success('支付成功',U('order/payresult'));
                } else {
                    $this->error('支付遇到问题');
             }
        }         /* 我的鉴定支付 */
        else {
            // 检查鉴定状态
            is_array($checkup_id) && $checkup_id = implode(',', $checkup_id);
            is_array($checkup_no) && $checkup_no = implode(',', $checkup_no);
    
            $val_map = array();
            $val_map['id'] = array(
                    'in',
                    $checkup_id
            );
            $val_map['status'] = array(
                    'EGT',
                    1
            );
            $M_checkup->where($val_map)->find() && $this->error('鉴定单中出现重复支付,请重试');
    
            (double) $money > (double) $member_info['amount'] && $this->error('余额不足');
            $result = $M_member->where(array(
                    'uid' => UID
            ))->setDec('amount', $money);
    
            if ($result) {
    
                $payment_log_data['checkup_id'] = $checkup_id;
                $payment_log_data['checkup_no'] = $checkup_no;
                $payment_log_data['log'] = '鉴定单号：' . $checkup_no . '支付' . ' 支付方式:'.$paycode;
                
                $bill_data['title'] = '鉴定支付' . $checkup_no;
                $bill_data['desc'] = '鉴定单号：' . $checkup_no . ' 金额:' . $price . ' 支付方式:'.$paycode;                
    
                $payment_log_id = $M_payment_log->add($payment_log_data);
                M('bill')->add($bill_data);
    
                $map = array();
                $map['id'] = array(
                        'in',
                        $checkup_id
                );    
                $M_checkup->where($map)->save(array(
                        'pay_time'=>NOW_TIME,
                        'status' => 1,
                        'payment_log_id' => $payment_log_id
                ));
                addMessage(2,'付款通知','您的鉴定单'.$checkup_no.'支付成功',UID);
                $this->success('支付成功',U('order/payresult'));
            } else {
                $this->error('支付遇到问题');
            }
        }
    }
}
?>